Essential App Engine: Building High-Performance Java Apps with Google App Engine (Eiji Yamane's Library) by Adriaan de Jonge

Essential App Engine: Building High-Performance Java Apps with Google App Engine (Eiji Yamane's Library) by Adriaan de Jonge

Author:Adriaan de Jonge
Language: eng
Format: epub
Publisher: Addison-Wesley
Published: 2012-08-22T04:00:00+00:00


* * *

01 package com.appspot.mail;

02

03 import java.io.IOException;

04

05 import javax.servlet.ServletException;

06 import javax.servlet.http.HttpServlet;

07 import javax.servlet.http.HttpServletRequest;

08 import javax.servlet.http.HttpServletResponse;

09

10 import org.antlr.stringtemplate.StringTemplate;

11 import org.antlr.stringtemplate.StringTemplateGroup;

12

13 import com.google.appengine.api.mail.MailService;

14 import com.google.appengine.api.mail.MailServiceFactory;

15 import com.google.appengine.api.mail.MailService.Message;

16

17 /**

18 * Low-level alternative to SendConfirmationJMServlet.

19 */

20 public class SendConfirmationLLServlet extends HttpServlet {

21

22 private static final String SENDER =

23 "No Reply <[email protected]>";

24 private static final StringTemplateGroup templates =

25 new StringTemplateGroup("mail", "WEB-INF/templates/mail");

26

27 private static final long serialVersionUID = 818089810592369246L;

28

29 protected void doPost(HttpServletRequest request,

30 HttpServletResponse response)

31 throws ServletException, IOException {

32

33 String recipient = request.getParameter("recipient");

34 String thread = request.getParameter("thread");

35 String url = request.getParameter("url");

36 String message = request.getParameter("message");

37

38 String subject = createSubject(thread);

39 String body = createBody(url, thread);

40 String htmlBody = createHtmlBody(url, thread);

41 String attachment = createAttachment(message);

42

43 sendMail(recipient, subject, body, htmlBody, attachment);

44 }

45

46 private void sendMail(String recipient, String subject,

47 String body, String htmlBody, String attachment)

48 throws IOException {

49 MailService mailService = MailServiceFactory.getMailService();

50 Message mail = new Message(SENDER, recipient, subject, body);

51 mail.setHtmlBody(htmlBody);

52 mail.setAttachments(new MailService.Attachment("message.txt",

53 attachment.getBytes()));

54 mailService.send(mail);

55 }

56

57 private String createBody(String url, String thread) {

58 StringTemplate body =

59 templates.getInstanceOf("confirmation-body");

60 body.setAttribute("url", filter(url));

61 body.setAttribute("thread", filter(thread));

62 return body.toString();

63 }

64

65 private String createHtmlBody(String url, String thread) {

66 StringTemplate body =

67 templates.getInstanceOf("confirmation-html-body");

68 body.setAttribute("url", filter(url));

69 body.setAttribute("thread", filter(thread));

70 return body.toString();

71 }

72

73 private String createSubject(String thread) {

74 StringTemplate subject =

75 templates.getInstanceOf("confirmation-subject");

76 subject.setAttribute("thread", filter(thread));

77 return subject.toString();

78 }

79

80 private String createAttachment(String message) {

81 StringTemplate subject =

82 templates.getInstanceOf("confirmation-attachment");

83 subject.setAttribute("message", filter(message));

84 return subject.toString();

85 }

86

87 private String filter(String text) {

88 if (text == null)

89 return "";

90 return text.replaceAll("<?>?", "");

91 }

92 }



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.